package com.yantong.service.impl;

import com.yantong.constant.BusinessCode;
import com.yantong.exception.BDException;
import com.yantong.mapper.CentreMapper;
import com.yantong.pojo.Centre;
import com.yantong.utils.PageUtils;
import com.yantong.mapper.AchieveConfigMapper;
import com.yantong.pojo.AchieveConfig;
import com.yantong.pojo.AchieveConfigExample;
import com.yantong.service.AchieveConfigService;
import com.yantong.service.AchieveConfigService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author kanghaixin
 * @date 2018/9/20.17:56
 */
@Service
public class AchieveConfigServiceImpl implements AchieveConfigService {

    @Autowired
    private AchieveConfigMapper achieveConfigMapper;
    @Autowired
    private CentreMapper centreMapper;

    @Override
    public PageUtils<AchieveConfig> listAchieveConfigs(Long centreId, int offset, int limit) {
        PageUtils<AchieveConfig> pageUtils = new PageUtils<>();

        AchieveConfigExample example = new AchieveConfigExample();
        if (centreId != null) {
            example.or().andCentreIdEqualTo(centreId);
        }
        int total = achieveConfigMapper.countByExample(example);
        if (total > 0) {
            pageUtils.setTotal(total);
            List<AchieveConfig> list = achieveConfigMapper.selectByExample(example, new RowBounds(offset, limit));
            for (AchieveConfig config : list) {
                if (config.getCentreId() == null) {
                    continue;
                }
                Centre centre = centreMapper.selectByPrimaryKey(config.getCentreId());
                if (centre == null) {
                    continue;
                }
                config.setCentreName(centre.getName());
            }
            pageUtils.setRows(list);
        }
        return pageUtils;
    }

    @Override
    public AchieveConfig getByKey(Long configId) {
        if (configId == null) {
            return null;
        }
        return achieveConfigMapper.selectByPrimaryKey(configId);
    }

    @Override
    public int save(AchieveConfig config) {
        if (config == null) {
            return 0;
        }
        if (!checkOrderNum(config.getOrderMin(), config.getOrderMax(), null, config.getCentreId())) {
            throw new BDException(BusinessCode.ACHIEVE_CONFIG_ORDERNUM_ERROR);
        }
        return achieveConfigMapper.insertSelective(config);
    }

    @Override
    public int updateConfig(AchieveConfig config) {
        if (config == null || config.getId() == null) {
            return 0;
        }
        if (!checkOrderNum(config.getOrderMin(), config.getOrderMax(), config.getId(), config.getCentreId())) {
            throw new BDException(BusinessCode.ACHIEVE_CONFIG_ORDERNUM_ERROR);
        }
        return achieveConfigMapper.updateByPrimaryKeySelective(config);
    }

    @Override
    public int remove(Long configId) {
        if (configId == null) {
            return 0;
        }
        return achieveConfigMapper.deleteByPrimaryKey(configId);
    }

    @Override
    public int getMinimal(Long centreId) {
        if (centreId == null) {
            return 0;
        }
        Integer result = achieveConfigMapper.getMinimal(centreId);
        if (result == null) {
            return 0;
        }
        return result;
    }

    @Override
    public AchieveConfig getByOrderNum(Long centreId, Integer num) {
        return achieveConfigMapper.getByOrderNum(centreId, num);
    }

    /**
     * 检查单量是否已经存在某个级别区间
     * @param min
     * @param max
     * @param configId
     * @param centreId
     * @return
     */
    private boolean checkOrderNum(int min, int max, Long configId, Long centreId){
        AchieveConfigExample example = new AchieveConfigExample();
        AchieveConfigExample.Criteria criteria = example.createCriteria();
        criteria.andOrderMinLessThanOrEqualTo(min)
                .andOrderMaxGreaterThanOrEqualTo(min)
                .andCentreIdEqualTo(centreId);
        if (configId != null) {
            criteria.andIdNotEqualTo(configId);
        }
        int total = achieveConfigMapper.countByExample(example);
        if (total >0) {
            return false;
        }
        example.clear();
        criteria = example.createCriteria();
        criteria.andOrderMinLessThanOrEqualTo(max)
                .andOrderMaxGreaterThanOrEqualTo(max)
                .andCentreIdEqualTo(centreId);
        if (configId != null) {
            criteria.andIdNotEqualTo(configId);
        }
        total = achieveConfigMapper.countByExample(example);
        if (total >0) {
            return false;
        }

        example.clear();
        criteria = example.createCriteria();
        criteria.andOrderMinGreaterThanOrEqualTo(min)
                .andOrderMaxLessThanOrEqualTo(max)
                .andCentreIdEqualTo(centreId);
        if (configId != null) {
            criteria.andIdNotEqualTo(configId);
        }
        total = achieveConfigMapper.countByExample(example);
        if (total >0) {
            return false;
        }
        return true;
    }
}
